package com.navinfo.tripanalysis.service.impl;

import com.navinfo.tripanalysis.common.arithmetic.*;
import com.navinfo.tripanalysis.common.arithmetic.common.DrivingData;
import com.navinfo.tripanalysis.common.arithmetic.manager.IArithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterEventData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.pojo.AlgorithmChainCommand;
import com.navinfo.tripanalysis.pojo.Point0200;
import com.navinfo.tripanalysis.pojo.Point;
import com.navinfo.tripanalysis.service.AbstractAlgorithmChainService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 0200算法链调用，并输出行程及事件
 * @author wangshuai
 */
public class AlgorithmChainService0200 extends AbstractAlgorithmChainService {

    public AlgorithmChainService0200(Map algorithmConfig) {
        super(algorithmConfig);

        List<Class<? extends IArithmetic>> excludes = new ArrayList<>();
        excludes.add(SharpAcceleration.class);//急加速
        excludes.add(SharpDeceleration.class); //急减速
        excludes.add(GasSharp.class); //急踩油门
        excludes.add(SharpTurning.class);//急转弯
        excludes.add(CoastNeutralGear.class);//空挡滑行
        excludes.add(Brake.class); //制动相关算法
        excludes.add(BrakeNormal.class); //制动相关算法
        excludes.add(BrakeHighSpeed.class); //制动相关算法
        excludes.add(CoastInGear.class); //在挡滑行
        excludes.add(CarStart.class);//启步,冷车启步，停车
        excludes.add(GasSteady.class);//油门稳定性
        excludes.add(VehicleEstimatedLoad.class); //整车估算载荷
        excludes.add(DrivingCity.class);//城市行驶 非城市
        excludes.add(DrivingEco.class); //经济行驶
        excludes.add(DrivingSlope.class);//坡路行驶
        excludes.add(Gear.class);//挡位算法
        excludes.add(ClutchSwitch.class);//离合开关

        excludes.add(SharpAcceleration_DF.class);//东风急加速
        excludes.add(SharpDeceleration_DF.class); //东风急减速
        excludes.add(SharpTurning_DF.class);//东风急转弯
        excludes.add(DrivingCold_DF.class);//东风冷车运行
        excludes.add(GasLargeAndFull_DF.class);//东风大油门和全油门
        excludes.add(CoastNeutralGear_DF.class);//东风空挡滑行
        excludes.add(CoastFlameout_DF.class);//东风熄火滑行

        //去除0200统计会很不准的算法
        arithmeticManager.delArithmetic(excludes);

        //根据0200统计对应的事件次数
        arithmeticManager.addArithmetic(new NumberBy0200());
    }

    @Override
    protected OuterStatisticData collectTripData(DrivingData drivingData, long tid, Map<Long, Integer> tileLcMapping) {
        OuterStatisticData statisticData = super.collectTripData(drivingData, tid, tileLcMapping);
        statisticData.setStype(OuterStatisticData.S_TYPE_0200);
        return statisticData;
    }

    @Override
    protected void collectEventData(AlgorithmChainCommand command, long tid, OuterEventData eventData) {
        super.collectEventData(command, tid, eventData);
        eventData.setStype(OuterEventData.S_TYPE_0200);
    }

    /**
     * 执行方法
     * @param command 算法链执行的命令对象
     */
    @Override
    public void execute(AlgorithmChainCommand command) {
        List<Point0200> points = command.getPoints0200();

        if (null!=points && points.size()>0) {
            DrivingData drivingData = command.getDrivingData();
            int lastIndex = points.size() - 1;
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                if (i == lastIndex) {
                    dealLastPoint(command, drivingData, point);
                } else {
                    dealPoint(command, drivingData, point);
                }
            }

            if(command.isHandleCrossDay()) {
                handleCrossDay(command.getOuterData().getOutStatistics(), drivingData);
            }
            command.getOuterData().setDrivingData(drivingData);
        } else {
            logger.error("原始点数据为空！");
        }
    }
}

